<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑新闻</title>
</head>
<body>
<h1>编辑新闻</h1>
<button id="back" type="button">返回</button>
<div id="content">
    <form>

    </form>
</div>

<script src="../resources/jquery-2.1.4.js"></script>
<script src="../resources/common.js"></script>
<script>
    $(function () {
        init();
        $("#back").click(function () {
            window.location.href = "index.html";
        });
    });

    function init() {
        var cotent = $('#content');
        var form = cotent.find('form');
        if (params.id) {
            $.ajax({
                url: baseUrl+"/news/"+params.id,
                type: 'GET',
                dataType: 'json',
                success: function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        var model = result.model;
                        view(form,model);
                    } else {
                        alert(result.msg);
                    }
                }
            });
        }else {
            view(form);
        }
    }

    function view(form,model) {
        form.append('<div>' +
            '<label for="title">标题:</label>' +
            '<input type="text" id="title" name="title" required value="'+(model? model.title : '')+'"/>' +
            '</div>');
        form.append('<div>' +
            '<label for="newsContent">内容:</label><br/>' +
            '<textarea id="newsContent" name="newsContent" required' +
            'style="width: 600px;height:300px;">'+ (model? model.newsContent : '') +'</textarea>' +
            '</div>');
        form.append('<div>' +
            '<label for="publisher">发布者:</label>' +
            '<input type="text" id="publisher" name="publisher" required value="'+ (model? model.publisher : '') +'"/>' +
            '</div>');
        if (model){
            form.append('<input type="hidden" id="id" name="id" value="'+ model.id +'"/><br/>');
        }
        form.append($('<input id="submit" type="button" value="保存"/>').click((model? edit : add)));
    }

    function add() {
        var title = $('#title').val();
        if (title == '') {
            return alert('请输入新闻标题');
        }
        var newsContent = $('#newsContent').val();
        if (newsContent == '') {
            return alert('请输入新闻内容');
        }
        var publisher = $('#publisher').val();
        if (publisher == '') {
            return alert('请输入发布者');
        }
        $.ajax({
            url: baseUrl + "/news",
            data: JSON.stringify({
                title: title,
                newsContent: newsContent,
                publisher: publisher
            }),
            contentType: "application/json;charset=UTF-8",
            type: 'POST',
            dataType: 'json',
            error: function (jqXHR, textStatus, errorThrown) {
                //请求失败时调用此函数
                //三个参数:jqXHR 对象、错误信息、（可选）捕获的异常对象
                //如果发生了错误，错误信息（第二个参数）除了得到null之外，还可能是"success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror"
                console.log("-----------error-----------");
                console.log(arguments);
                alert("请求失败：" + textStatus);
            },
            success: function (result, textStatus, jqXHR) {
                // data 根据dataType参数进行处理后的数据
                console.log("-----------success-----------");
                console.log(arguments);
                if (result.code == 0) {
                    alert("保存成功");
                    window.location.href = "index.html";
                } else {
                    alert("保存失败:" + result.code + " : " + result.msg);
                }
            }
        });
    }


    function edit() {
        var title = $('#title').val();
        if (title == '') {
            return alert('请输入新闻标题');
        }
        var newsContent = $('#newsContent').val();
        if (newsContent == '') {
            return alert('请输入新闻内容');
        }
        var publisher = $('#publisher').val();
        if (publisher == '') {
            return alert('请输入发布者');
        }
        $.ajax({
            url: baseUrl + "/news/"+params.id,
            data: JSON.stringify({
                id: $('#id').val(),
                title: title,
                newsContent: newsContent,
                publisher: publisher
            }),
            contentType: "application/json;charset=UTF-8",
            type: 'PATCH',
            dataType: 'json',
            error: function (jqXHR, textStatus, errorThrown) {
                alert("请求失败：" + textStatus);
            },
            success: function (result, textStatus, jqXHR) {
                if (result.code == 0) {
                    alert("保存成功");
                    window.location.href = "index.html";
                } else {
                    alert("保存失败:" + result.code + " : " + result.msg);
                }
            }
        });
    }

</script>
</body>
</html>
